package nio;

import com.sun.corba.se.spi.activation.Server;
import com.sun.org.apache.bcel.internal.generic.Select;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

/**
 * Created by luzl on 4/8/2017.
 */
public class ServerTest {
    private ByteBuffer buffer=ByteBuffer.allocate(1000);

    public static void main(String[] args){
        ServerTest test=new ServerTest();
        test.start();
    }

    private void start(){
        try {
            //Initialize the server
            ServerSocketChannel channel=ServerSocketChannel.open();//get channle
            ServerSocket socket=channel.socket(); //get socket from channel
            SocketAddress socketAddress=new InetSocketAddress(8080); //get socket address object
            socket.bind(socketAddress); //bind socket address to socket

            Selector selector=Selector.open();//get selector
            channel.configureBlocking(false); //channel set to false
            channel.register(selector,SelectionKey.OP_ACCEPT); //channel register selctor and key

            int readykeys=selector.select();
            if (readykeys>0){
                while (true){
                    Set<SelectionKey> selectionKeys=selector.selectedKeys();
                    Iterator<SelectionKey> selectionKeyIterator=selectionKeys.iterator();
                    while(selectionKeyIterator.hasNext()){
                        SelectionKey selectionKey=selectionKeyIterator.next();
                        if (selectionKey.isAcceptable()){
                            System.out.println("Acceptable");
                            ServerSocketChannel server=(ServerSocketChannel) selectionKey.channel();
                            SocketChannel serverSocket=server.accept();
                            if (serverSocket!=null) {
                                buffer.clear();
                                buffer.put("Hello There!".getBytes());
                                buffer.flip();
                                serverSocket.write(buffer);//write the data of buffer to serverSocket
                            }
                        }

                        if (selectionKey.isReadable()){
                            System.out.println("Readable");
                            SocketChannel readChannel=(SocketChannel) selectionKey.channel();
                            buffer.clear();
                            int count=readChannel.read(buffer);//read data from channel to buffer
                            buffer.flip();
                            while (count>0){
                                String content=new String(buffer.array());
                                System.out.println(content);
                            }
                            if (count<0){
                                readChannel.close();
                            }
                        }
                        selectionKeyIterator.remove();
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
